<%@ Page Title="Applications &raquo; ResourceBlender" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Applications.aspx.cs" Inherits="Applications" EnableEventValidation="false" %>
<%@ MasterType VirtualPath="~/MasterPage.master" %>

<asp:Content ID="contentHead" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="contentMain" ContentPlaceHolderID="cntMain" Runat="Server">
    <asp:SqlDataSource ID="dsMain" runat="server"
         ConnectionString="<%$ ConnectionStrings:Default %>"
         ProviderName="<%$ ConnectionStrings:Default.ProviderName %>"
         SelectCommand="SELECT name, id, displayname FROM applications">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="dsBundles" runat="server"
         ConnectionString="<%$ ConnectionStrings:Default %>"
         ProviderName="<%$ ConnectionStrings:Default.ProviderName %>">
    </asp:SqlDataSource>
    <asp:ScriptManager ID="scrBundles" runat="server">
        <Scripts>
            <asp:ScriptReference Path="~/js/applications.js" />
        </Scripts>
    </asp:ScriptManager>
    <asp:MultiView ID="mltMain" runat="server" ActiveViewIndex="1">
        <asp:View ID="viwManageApplications" runat="server">
            <h1><asp:Label ID="lblManageApplications" runat="server" Text="Manage Applications"></asp:Label></h1>
            <div class="breadcrumbs">
                <asp:HyperLink ID="lnkNavHome" runat="server" NavigateUrl="~/Default.aspx">ResourceBlender</asp:HyperLink> &gt; Applications
            </div>
            <asp:GridView ID="grdApplications" runat="server" AllowPaging="True" 
                AllowSorting="True" AutoGenerateColumns="False" CssClass="nicetable" 
                DataSourceID="dsMain" OnRowCommand="grdApplications_RowCommand" Width="100%">
                <PagerSettings PageButtonCount="7" />
                <Columns>
                    <asp:BoundField DataField="displayname" HeaderText="Application" 
                        SortExpression="displayname" />
                    <asp:TemplateField ItemStyle-CssClass="buttoncell" HeaderText="Edit">
                        <ItemTemplate>
                            <asp:ImageButton ID="btnEdit" runat="server" 
                                AlternateText='<%# "Edit " + Eval("displayname") %>' 
                                CommandArgument='<%# Eval("id") %>' CommandName="doedit" 
                                ImageUrl="~/images/edit.png" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ItemStyle-CssClass="buttoncell" HeaderText="Delete">
                        <ItemTemplate>
                            <asp:ImageButton ID="btnDelete" runat="server" 
                                AlternateText='<%# "Delete " + Eval("displayname") %>' 
                                CommandArgument='<%# Eval("id") %>' CommandName="confirmdelete" 
                                ImageUrl="~/images/delete.png" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <HeaderStyle CssClass="excelHead" />
                <AlternatingRowStyle CssClass="alt" />
                <EmptyDataTemplate>
                    <p>No applications have been added.</p>
                </EmptyDataTemplate>
            </asp:GridView>
            <asp:LinkButton ID="lnkAddApplication" runat="server" CssClass="lnkAdd" 
                OnClick="lnkAddApplication_Click">Add Application</asp:LinkButton>
        </asp:View>
        <asp:View ID="pnlAddApplication" runat="server">
            <div class="niceform">
                <h1><asp:Label ID="lblEditApplications" runat="server" Text="Add Application"></asp:Label></h1>
                <div class="breadcrumbs">
                    <asp:HyperLink ID="lnkNavAddHome" runat="server" NavigateUrl="~/Default.aspx">ResourceBlender</asp:HyperLink> &gt; <asp:HyperLink ID="lnkNavAddApplications" runat="server" NavigateUrl="~/Applications.aspx">Applications</asp:HyperLink> &gt; <asp:Literal runat="server" ID="litNavAddAddApplication" Text="Add Application"></asp:Literal>
                </div>
                <fieldset>
                    <legend>Application Options</legend>
                    <div class="formrow block">
                        <asp:Label ID="lblAppName" runat="server" AssociatedControlID="txtAppName" 
                            Text="Application Name"></asp:Label>
                        <asp:TextBox ID="txtAppName" runat="server" ValidationGroup="addapplication"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="vldAppName" runat="server" 
                            ControlToValidate="txtAppName" Display="Dynamic" SetFocusOnError="true"
                            ErrorMessage="You must enter the application name" ValidationGroup="addapplication"
                            CssClass="validationerror"></asp:RequiredFieldValidator>
                    </div>
                    <div class="formrow block">
                        <asp:Label ID="lblDisplayName" runat="server" AssociatedControlID="txtDisplayName" 
                            Text="Display Name"></asp:Label>
                        <asp:TextBox ID="txtDisplayName" runat="server" ValidationGroup="addapplication"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="vldDisplayName" runat="server" 
                            ControlToValidate="txtDisplayName" Display="Dynamic" SetFocusOnError="true" ValidationGroup="addapplication"
                            ErrorMessage="You must enter a display name" CssClass="validationerror"></asp:RequiredFieldValidator>
                    </div>
                </fieldset>
                <fieldset>
                    <legend>Languages</legend>
                    <asp:UpdatePanel ID="updLanguages" runat="server">
                        <ContentTemplate>
                            <div class="formrow block">
                                <div class="formcol">
                                    <asp:Label ID="lblLanguages" runat="server"
                                        AssociatedControlID="lstSelectedLangs" Text="Selected Languages"></asp:Label>
                                    <br />
                                    <asp:ListBox ID="lstSelectedLangs" runat="server" Width="20em" Rows="10" 
                                        CausesValidation="True" SelectionMode="Multiple" 
                                        ValidationGroup="vldLanguages"></asp:ListBox>
                                </div>
                                <div class="formcol" style="margin-top: 7em;">
                                    <asp:Button ID="btnAddLang" runat="server" Text="&lt;&lt;" 
                                        EnableViewState="False" CssClass="block" OnClick="btnAddLang_Click" />
                                    <asp:Button ID="btnRemoveLang" runat="server" Text="&gt;&gt;" 
                                        EnableViewState="False" CssClass="block" OnClick="btnRemoveLang_Click" />
                                </div>
                                <div class="formcol">
                                    <asp:Label ID="lblAvailableLanguages" runat="server" 
                                        AssociatedControlID="lstAvailableLangs" Text="Available Languages"></asp:Label>
                                    <br />
                                    <asp:ListBox ID="lstAvailableLangs" runat="server" Width="20em" Rows="10" 
                                        SelectionMode="Multiple"></asp:ListBox>
                                </div>
                                <div class="clear"></div>
                                <asp:UpdateProgress ID="prgLanguages" runat="server" 
                                    AssociatedUpdatePanelID="updLanguages" 
                                    DisplayAfter="100">
                                    <ProgressTemplate>
                                        <div class="centered"><asp:Image ID="imgLoadingLanguages" runat="server" ImageUrl="~/images/ajax-loader.gif" AlternateText="Loading..." /></div>
                                    </ProgressTemplate>
                                </asp:UpdateProgress>
                                <asp:CustomValidator ID="vldHasLanguages" runat="server" 
                                    ControlToValidate="lstSelectedLangs"  Display="Dynamic"
                                    ErrorMessage="At least one language must be selected" 
                                    OnServerValidate="validateHasLanguages" ValidateEmptyText="True" 
                                    ValidationGroup="vldLanguages"></asp:CustomValidator>
                            </div>
                            <div class="formrow block">
                                <asp:Label ID="lblDefaultLanguage" runat="server" Text="Default Language" CssClass="block"
                                    AssociatedControlID="cboDefaultLanguage"></asp:Label>
                                <asp:DropDownList ID="cboDefaultLanguage" runat="server">
                                </asp:DropDownList>
                            </div>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </fieldset>
                <fieldset>
                    <legend>Bundles</legend>
                    <asp:UpdatePanel ID="updBundles" runat="server" OnPreRender="updBundles_PreRender">
                        <ContentTemplate>
                            <h3>Existing Bundles</h3>
                            <asp:GridView ID="grdBundles" runat="server" AllowPaging="True" AllowSorting="True" 
                                AutoGenerateColumns="False" DataSourceID="dsBundles" CssClass="nicetable" DataKeyNames="id"
                                OnRowCommand="grdBundles_RowCommand" Width="100%">
                                <PagerSettings PageButtonCount="7" />
                                <Columns>
                                    <asp:BoundField DataField="name" HeaderText="Name" 
                                        SortExpression="name" />
                                    <asp:TemplateField ItemStyle-CssClass="buttoncell" HeaderText="Edit">
                                        <ItemTemplate>
                                            <asp:ImageButton ID="btnEdit" runat="server" 
                                                AlternateText='<%# "Edit " + Eval("name") %>' 
                                                CommandArgument='<%# Eval("id") %>' CommandName="edit" 
                                                ImageUrl="~/images/edit.png" />
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:ImageButton ID="btnUpdate" runat="server" 
                                                AlternateText="Save"
                                                CommandName="update" 
                                                ImageUrl="~/images/save.png" />
                                            <asp:ImageButton ID="btnCancelEdit" runat="server" 
                                                AlternateText="Cancel" 
                                                CommandName="cancel" 
                                                ImageUrl="~/images/undo.png" />
                                        </EditItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField ItemStyle-CssClass="buttoncell" HeaderText="Delete">
                                        <ItemTemplate>
                                            <asp:ImageButton ID="btnDelete" runat="server" 
                                                AlternateText='<%# "Delete " + Eval("name") %>' 
                                                CommandArgument='<%# Eval("id") %>' CommandName="deletebundle" 
                                                ImageUrl="~/images/delete.png" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <HeaderStyle CssClass="excelHead" />
                                <AlternatingRowStyle CssClass="alt" />
                                <EmptyDataTemplate>
                                    <p>No bundles have been added.</p>
                                </EmptyDataTemplate>
                            </asp:GridView>
                            <asp:UpdateProgress ID="prgUpdateBundles" runat="server" 
                                AssociatedUpdatePanelID="updBundles" 
                                DisplayAfter="100">
                                <ProgressTemplate>
                                    <div class="centered"><asp:Image ID="imgLoadingBundles" runat="server" ImageUrl="~/images/ajax-loader.gif" AlternateText="Loading..." /></div>
                                </ProgressTemplate>
                            </asp:UpdateProgress>
                            <asp:Label ID="lblNoBundles" runat="server" Visible="false"
                                    Text="Applications must contain at least one bundle" CssClass="block error"></asp:Label>
                            <h3>Add New Bundle</h3>
                            <div class="formrow block">
                                <asp:Label ID="lblNewBundle" runat="server" 
                                    AssociatedControlID="txtNewBundle" Text="Bundle Name"></asp:Label>
                                <asp:TextBox ID="txtNewBundle" runat="server" ValidationGroup="addbundles"></asp:TextBox>
                                <asp:RequiredFieldValidator
                                    ID="vldBundleNameRequired" runat="server" ErrorMessage="You must enter a name"
                                    ControlToValidate="txtNewBundle" Display="Dynamic" SetFocusOnError="true"
                                    CssClass="validationerror" ValidationGroup="addbundles"></asp:RequiredFieldValidator>
                                <asp:Label ID="lblAddError" runat="server" Visible="false"
                                    Text="Bundle already exists" CssClass="error"></asp:Label>
                            </div>
                            <asp:Button ID="btnAddBundle" runat="server" CommandName="bundleadd" 
                                CssClass="uniformbutton" EnableViewState="False" Text="Add Bundle" 
                                CausesValidation="False" OnClick="btnAddBundle_Click" 
                                ValidationGroup="addbundles" />
                            <br />
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </fieldset>
                <div class="formrow block actions">
                    <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="uniformbutton" ValidationGroup="addapplication"
                        OnClick="btnSave_Click" />
                </div>
            </div>
        </asp:View>
        <asp:View ID="pnlDeleteApplication" runat="server">
            <div class="breadcrumbs">
                <asp:HyperLink ID="lnkNavDeleteHome" runat="server" NavigateUrl="~/Default.aspx">ResourceBlender</asp:HyperLink> &gt; <asp:HyperLink ID="lnkNavDeleteApplications" runat="server" NavigateUrl="~/Applications.aspx">Applications</asp:HyperLink> &gt; Delete Application
            </div>
            <div class="niceform confirmation">
                <h1><asp:Label ID="lblConfirmDeletionHeading" runat="server" Text="Confirm Deletion" 
                        EnableViewState="False"></asp:Label></h1>
                <asp:Label ID="lblConfirmDelete" runat="server" 
                    Text="formatted text"></asp:Label>
                <br />
                <asp:Button ID="btnOk" runat="server" CommandName="confirmdelete" 
                    CssClass="uniformbutton" EnableViewState="False" onclick="btnOk_Click" 
                    Text="Delete Application" />
                <asp:Button ID="btnCancelDelete" runat="server" CommandName="canceldelete" 
                    CssClass="uniformbutton" EnableViewState="False" Text="Cancel" 
                    OnClick="btnCancelDelete_Click" />
            </div>
        </asp:View>
        <asp:View ID="viwError" runat="server">
            <div class="niceform error">
                <h1>Error</h1>
                <asp:Label ID="lblErrorDescription" runat="server" 
                    Text="formatted text"></asp:Label>
            </div>
        </asp:View>
    </asp:MultiView>
</asp:Content>